<template>
	<view>
		<navbar title="我的地址"></navbar>
		<view class="address-box" v-if="addressList[0]">
			<view class="address-list bgf" v-for="(item, index) in addressList" :key="index" @click="orderPick(item)">
				<view class="fa fwb">
					<view>{{item.name}}</view>
					<view class="mobile">{{item.phone}}</view>
				</view>
				<view class="address minor">{{item.details}}</view>
				<view class="address-btn fa jcsb">
					<view class="fa" @click.stop="setDefault(item.accountAddressId)">
						<image class="pick-icon" src="../../static/pick-fill.png" mode="" v-if="item.isDefault == 1"></image>
						<image class="pick-icon" src="../../static/pick.png" mode="" v-else></image>
						<view>设为默认</view>
					</view>
					<view class="btn-box fa">
						<view class="fa" @click.stop="editAddress(item.accountAddressId)">
							<image src="../../static/edit.png" mode=""></image>
							<view>编辑</view>
						</view>
						<view class="delete fa" @click.stop="deleteAddress(item.accountAddressId)">
							<image src="../../static/delete.png" mode=""></image>
							<view>删除</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="nodata-box" v-if="hasData">
			<image class="nodata-img" src="../../static/nodata.png" mode=""></image>
			<view class="nodata-prompt">暂无地址，请添加~</view>
		</view>
		<view class="operate-wrapper">
			<view class="operate-place"></view>
			<div class="operate-box fa">
				<view class="addbtn fa jcc fwb" @click="addAddress">添加收货地址</view>
			</div>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: 1,
				addressList: [],
				hasData: 0
			};
		},
		onLoad(options) {
			if(options.type) {
				this.type = options.type
			}
		},
		onShow() {
			this.getAddress()
		},
		onPullDownRefresh() {
			this.getAddress()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1000)
		},
		methods: {
			// 获取地址
			getAddress() {
				this.hasData = 0
				this.api.addressListFunc().then(res => {
					if(res.code == 200) {
						this.addressList = res.data
						if(res.data.length > 0) {
							this.hasData = 0
						}else {
							this.hasData = 1
						}
					}
				})
			},
			// 下单选择地址
			orderPick(item) {
				// type,2,3,4 商城下单，帮我取送选择取件收件地址
				if(this.type == 2) {
					uni.$emit('selectAddress', item);
					uni.navigateBack()
				}else if(this.type == 3) {
					uni.$emit('selectSendAddress', item);
					uni.navigateBack()
				}else if(this.type == 4) {
					uni.$emit('selectReceiveAddress', item);
					uni.navigateBack()
				}
			},
			// 添加地址
			addAddress() {
				uni.navigateTo({
					url: '/pages/mine/address_add'
				})
			},
			// 设为默认地址
			setDefault(id) {
				this.api.addressUpdateFunc({
					accountAddressId: id,
					isDefault: 1
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: res.msg
						})
						this.getAddress()
					}else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(err => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				})
			},
			// 编辑地址
			editAddress(id) {
				uni.navigateTo({
					url: '/pages/mine/address_add?id=' + id
				})
			},
			// 删除地址
			deleteAddress(id) {
				uni.showModal({
					title: '提示',
					content: '确认删除该地址吗',
					success: result => {
						if(result.confirm) {
							this.api.addressDelFunc({
								id: id
							}).then(res => {
								if(res.code == 1) {
									uni.showToast({
										title: res.msg
									})
									this.getAddress()
								}else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.address-box{
		margin-top: 16rpx;
		padding-bottom: 2rpx;
		.address-list{
			margin-bottom: 16rpx;
			padding: 24rpx 26rpx;
			.mobile{
				margin-left: 10rpx;
			}
			.address{
				margin-top: 10rpx;
			}
			.address-btn{
				margin-top: 22rpx;
				padding-top: 22rpx;
				border-top: 2rpx solid #eee;
				color: #666;
				.pick-icon{
					margin-right: 10rpx;
					width: 36rpx;
					height: 36rpx;
				}
				.btn-box{
					image{
						margin-right: 10rpx;
						width: 36rpx;
						height: 36rpx;
					}
					.delete{
						margin-left: 40rpx;
					}
				}
			}
		}
	}
	.operate-wrapper{
		.operate-place{
			height: calc(env(safe-area-inset-bottom) + 140rpx);
		}
		.operate-box{
			position: fixed;
			bottom: 0;
			bottom: env(safe-area-inset-bottom);
			left: 0;
			right: 0;
			padding: 0 30rpx;
			height: 110rpx;
			background: #f8f8f8;
			z-index: 999;
			.addbtn{
				width: 100%;
				height: 88rpx;
				border-radius: 44rpx;
				color: #fff;
				background: linear-gradient( 270deg, #6CE083 0%, #3DBF63 100%);
			}
		}
	}
</style>
